//
//  moreViewController.m
//  timemovie
//
//  Created by Yiqiao on 15/7/17.
//  Copyright (c) 2015年 Yiqiao. All rights reserved.
//

#import "moreViewController.h"
#import "ViewControllerMore.h"
#import "UIViewExt.h"
#import "SDImageCache.h"
#define w [UIScreen mainScreen].bounds.size.width
#define h [UIScreen mainScreen].bounds.size.height
#define navH 64;
#define barH 49;
@interface moreViewController ()

@end

@implementation moreViewController

- (void)_createTable{
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
     [self.view addSubview:_tableView];
}

- (void)viewWillAppear:(BOOL)animated{
  //  NSLog(@"222");
    [_tableView reloadData];
    
}

- (void)_loadData{
    title = @[@"clear",@"awp",@"m4",@"ak",@"p90",@"mp7"];
    imageNames = @[@"moreClear@2x",@"moreScore@2x",@"moreVersion@2x",@"moreBusiness@2x",@"more@2x",@"moreAbout@2x"];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return title.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"alert" message:@"whether clear" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"confirm", nil];
        [alert show];
    }

}

- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 1) {
        SDImageCache *cache = [SDImageCache sharedImageCache];
        
        [cache clearDisk];
        //NSLog(@"111");
        
       // CGFloat size = [cache getSize];
        //NSLog(@"%@",[NSString stringWithFormat:@"%0.1fM",size/1000/1000]);
        [_tableView reloadData];
        //   NSLog(@"%@",[NSString stringWithFormat:@"%0.1fM",size/1000/1000]);
    }

}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indenty = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indenty];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indenty];
        cell.backgroundColor = [UIColor clearColor];
        UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
        imageView1.tag = 1;
        [cell.contentView addSubview:imageView1];
        UILabel *title1 = [[UILabel alloc ]initWithFrame:CGRectMake(imageView1.right+30, 15, 200, 40)];
        title1.tag = 2;
        title1.font = [UIFont boldSystemFontOfSize:18];
        title1.textColor = [UIColor whiteColor];
        [cell.contentView addSubview:title1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row == 0) {
            UILabel *cache = [[UILabel alloc ]initWithFrame:CGRectMake(w-70, 15,60, 30)];
            cache.textAlignment = NSTextAlignmentCenter;
            cache.tag = 3;
            cache.font = [UIFont boldSystemFontOfSize:18];
            cache.textColor = [UIColor whiteColor];
            [cell.contentView addSubview:cache];
            
        }
    }
    UIImageView *imageView2 = (UIImageView*)[cell.contentView viewWithTag:1];
    UILabel *label = (UILabel *)[cell.contentView viewWithTag:2];
    imageView2.image = [UIImage imageNamed:imageNames[indexPath.row]];
    label.text = title[indexPath.row];
    UILabel *cache = (UILabel*)[cell.contentView viewWithTag:3];
    SDImageCache *ca = [[SDImageCache sharedImageCache]init];
    CGFloat size = [ca getSize];
    cache.text = [NSString stringWithFormat:@"%0.1fM",size/1000/1000];
    
    return cell;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createTable];
    [self _loadData];
    
    NSString *homePath = NSHomeDirectory();
    NSLog(@"%@",homePath);
    
    self.title = @"More";
    UIBarButtonItem *settings = [[UIBarButtonItem alloc]initWithTitle:@"Settings" style:UIBarButtonItemStylePlain target:self action:@selector(barButtonAct:)];
    self.navigationItem.rightBarButtonItem = settings;
    // Do any additional setup after loading the view.
}
- (void)barButtonAct:(UIBarButtonItem *)button{
    ViewControllerMore *morePage = [[ViewControllerMore alloc]init];
    morePage.view.backgroundColor = [UIColor greenColor];
    [UIView transitionWithView:self.view duration:0.5 options:UIViewAnimationOptionTransitionCurlDown animations:nil completion:nil];
    
    
    [self.navigationController pushViewController:morePage animated:NO];
   // [UIView transitionWithView:self.view duration:0.5 options:UIViewAnimationOptionTransitionCurlDown animations:nil completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
